package program2;
import java.net.*;
import java.io.*;
import java.util.Scanner;
import program1.MyHashMay;
import program1.filein;
public class client {//客户端
	public static void main(String[] args) throws UnknownHostException, IOException
	{
		Scanner scan = new  Scanner(System.in);	
		Socket socket = new Socket("127.0.0.1",10086);
		FileOutputStream fos = new FileOutputStream("./hotel message/message.txt ",true);//创建输入流
		OutputStream os = socket.getOutputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		int data;//用于读取字符
		StringBuilder buffer = new StringBuilder();//实现字符的拼接
		String regex = "\\s";//定义正则表达式		
		while(true)//循环多次，进行多次传输
		{
			System.out.println("请输入：");	//发送信息
			String str = scan.nextLine();			
			String sign = "\n" ;//作为字符串的结尾传给服务端
			if (str.trim().isEmpty()) 
			{
			    System.out.println("输入不能为空，请重新输入");
			    continue;
			}
			String []word = str.split(regex);//切割
			if("end".equals(word[0]))//结束程序的命令
				break;
			os.write(str.getBytes());//传输内容
			os.write(sign.getBytes());//传输结尾			
			os.flush();//刷新
			while((data=br.read())!=-1)//读取信息
			{
				buffer.append((char)data);	
				if(data=='\n')//检查是否读完一整个字符串
				{
					String receivedString = buffer.toString().trim();
					if (!receivedString.equals("end"))//设定end为读取停止的指令 
					{	               
						System.out.println(receivedString);
						buffer = new StringBuilder();//重置buffer	
					}
					else
					{
						buffer = new StringBuilder();//重置buffer
						break;
					}
						
				}
			}
		}
		fos.close();
		os.close();
		socket.close();
	}

}
